/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.performance.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumn;

import cgl.narada.transport.LinkPerformanceData;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Company: </p>
 * @author Gurhan Gunduz
 * @version 1.0
 */

public class TableView extends JFrame implements ActionListener {
  private JTable table;
//  private JPanel c=new JPanel();
  private JComponent c;
//  private Container c;
  private JButton button;
  private Vector v=new Vector();
  private Vector values;
  private Vector columnNames=new Vector();
  private JScrollPane scroller;
  private TableColumn column = null;
  private UIManager.LookAndFeelInfo looks[];
  private long[] lat;
  private Chart chart=new Chart("  ");
  Vector links=new Vector();
  Hashtable linkNames=new Hashtable();

  //constructor
  public TableView() {

    super("Monitoring Service");
    looks=UIManager.getInstalledLookAndFeels();
    try {
      UIManager.setLookAndFeel(
          looks[2].getClassName());
    }
    catch (Exception e) {}
    c=(JComponent)getContentPane();
    c.setDoubleBuffered(true);
    c.setLayout(new BorderLayout());
    button=new JButton("Refresh");
    JPanel panel = new JPanel();
    c.add(panel, BorderLayout.SOUTH);
    c.setBackground(Color.BLUE);
    c.setForeground(Color.red);
    panel.add(button);
    //panel.setBackground(Color.darkGray);
    button.setBackground(Color.gray);
    button.addActionListener(this);
    setSize(600,300);
//    table.setCellSelectionEnabled(true);


  }

  public void set(){
    show();
  }

  public void display(){

    //System.out.println("displayyyyyy");
    values=new Vector();
    columnNames.addElement("Link Name ");
    columnNames.addElement("Avg Latency");
    columnNames.addElement("Std Deviation");
    columnNames.addElement("Jitter");
//    columnNames.addElement("Throughput");
    columnNames.addElement("Loss Rate");
//    values.addElement(getValues());
//    values.addElement(getValues());
//    values.addElement(getValues());
//    values.addElement(getValues());
//    values.addElement(getValues());

    table =new JTable(values, columnNames);
    addListener();
    table.setBackground(Color.white);
    table.setForeground(Color.black);
    column= table.getColumnModel().getColumn(0);
    column.setPreferredWidth(200);
    table.doLayout();
    scroller=new JScrollPane(table);
    scroller.setDoubleBuffered(true);
    c.add(scroller, BorderLayout.CENTER);
    validate();

  }

//creating a new  vector is important here...
  public Vector getValues(){
    v=new Vector();
    v.addElement("NA");
    v.addElement("NA");
    v.addElement("NA");
    v.addElement("NA");
    v.addElement("NA");
    return v;
  }

//  public void actionPerformed(ActionEvent e){
//
//    scroller.remove(table);
//    table =new JTable(values, columnNames);
//    table.setBackground(Color.white);
//    table.setForeground(Color.black);
//    column= table.getColumnModel().getColumn(0);
//    column.setPreferredWidth(200);
//    scroller.setViewportView(table);
//    validate();
//
//  }

  public void actionPerformed(ActionEvent e) {
//    chart=new Chart();
//    chart.show();

  }
  public void chartShow(){
    chart.show();
  }

  public void refreshChart(){
    chart.setLatencies(lat);
    chart.repaint();

  }

  public void setMonitoringData(LinkPerformanceData[] lpd, String linkId){
    if (linkNames.containsKey(linkId)){
      chart=(Chart)linkNames.get(linkId);
      chart.setMonitoringData(lpd);
    }else{
      chart=new Chart(linkId);
      linkNames.put(linkId,chart);
      chart.setMonitoringData(lpd);
//      chart.show();
    }

  }



  public void refresh(){

//    scroller.remove(table);
    table =new JTable(values, columnNames);
    table.doLayout();
    //System.out.println("row count "+table.getRowCount());
    table.setDoubleBuffered(true);
    addListener();
//    table.setBackground(Color.white);
//    table.setForeground(Color.black);

    column= table.getColumnModel().getColumn(0);
    column.setPreferredWidth(200);
    scroller.add(table);
    scroller.setViewportView(table);
    validate();

    //table.setGridColor(Color.blue);
    //c.add(scroller, BorderLayout.CENTER);


  }




  public void setValues(Vector val){
    //values=new Vector();
//    System.out.println("size1 "+values.size());
//    values.removeAllElements();
    values=val;
//    System.out.println("size3 "+values.size());
  }


  public void setLatencies(long[] values){
    lat = new long[values.length];
    for (int i = 0; i < values.length; i++) {
      lat[i]=values[i];
    }
    chart.setLatencies(lat);
  }

  public void setLatency(long _latency){
    //chart.setLatency(_latency);
  }

  public void addListener(){
    ListSelectionModel rowS = table.getSelectionModel();
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    rowS.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
        //Ignore extra messages.
        if (e.getValueIsAdjusting())
          return;

        ListSelectionModel lsm = (ListSelectionModel) e.getSource();
        if (lsm.isSelectionEmpty()) {
//          System.out.println("No rows are selected.");
        }
        else {
          int selectedRow = lsm.getMinSelectionIndex();
//          System.out.println("Row " + selectedRow
//                             + " is now selected.");
          String linkId=(String)table.getValueAt(selectedRow,0);
          if (linkNames.containsKey(linkId)){
            displayChart(linkId);
          }

        }
      }

    });
  }

  public void displayChart(String linkId){
    chart=(Chart)linkNames.get(linkId);
    chart.show();
  }

//  public void paint(Graphics g){
//
//  }

  public static void  main(String args[]){
    UIManager.LookAndFeelInfo looks[];
    looks=UIManager.getInstalledLookAndFeels();

    try {
    UIManager.setLookAndFeel(
        looks[2].getClassName());
    } catch (Exception e) { }
    TableView t=new TableView();
    t.set();
    t.display();
    long[] lat = {
        150, 130, 120, 100, 110, 100, 110, 122, 134};
    t.setLatencies(lat);
    //t.chartShow();
    //t.refreshChart();
    long[] lat2 = {
        10, 10, 10, 100, 110, 100, 110, 122, 134};
    t.setLatencies(lat2);
    //t.repaint();
//t.refreshChart();
  }
}









